﻿using System;
using System.Collections.Generic;
using ERP.Framework.Components.DatabaseHelper.Providers;
using ERP.Framework.Components.DatabaseHelper;
using System.Data;
using ERP.Business.DataTypes.Users.Credentials;


namespace ERP.Business.Users.Credentials
{
    /// <summary>
    /// Credentials manager.
    /// Check user credentials
    /// </summary>
    public sealed class CredentialsManager
    {
        private const string CHECK_USER_CREDENTIALS_SP = "SP_CHECK_USER_CREDENTIALS";
        /// <summary>
        /// Get user's credentials for a certain module
        /// </summary>
        /// <param name="username">username</param>
        /// <param name="password">password</param>
        /// <param name="moduleID">module id</param>
        /// <returns><see cref="CredentialStatus"/></returns>
        public static CredentialStatus GetUserCredential(string username, string password, string moduleID)
        {
            CredentialStatus cresult = CredentialStatus.AccessDenied;

            GeneralDatabaseProvider con = new GeneralDatabaseProvider(DatabaseConstants.AdministrationDatabase);
            ParametersCollection p = new ParametersCollection();
            p.AddParameter(new Parameter("@USERNAME", username, ParameterType.String));
            p.AddParameter(new Parameter("@PASSWORD", password, ParameterType.String));
            p.AddParameter(new Parameter("@MODULE_ID", moduleID, ParameterType.String));
            object result = con.ReadScalar(ERP.Framework.Components.DatabaseHelper.CommandType.StoredProcedure, CHECK_USER_CREDENTIALS_SP, p);
            bool bolRes = false;

            if (result != null && bool.TryParse(result.ToString(), out bolRes) && bolRes)
            {
                //set initial credentioal to ReadOnly
                cresult = CredentialStatus.ReadOnly ;
            }

            return cresult;

        }
    }
}
